# naslog-lib.pl
# Common local naslog functions

sub get_raid_log
{
	my @raidlogarr=();
	my $raidtype = `cat /etc/raidtype`;
	if($raidtype =~ /lsi/) {
		`/sbin/MegaCli -AdpEventLog -GetEvents -f /tmp/raidlog -a0`;
	}	
	
	open(FH, "/tmp/raidlog");
	my @raidlog=<FH>;
	close(FH);
	`rm -f /tmp/raidlog`;
	my $logline=@raidlog;
	#`echo $logline >> /tmp/rl`;
	for($i=0; $i<$logline; $i++) {
		#$raidlog[$i] =~ s/\r|\n//g;
		my $datetime=$logdesc=$logdata="";
		if($raidlog[$i] !~ /^seqNum:/) {
			next;
		}
		else{
			#print $raidlog[$i];
			for($j=$i+1; $j<$logline; $j++) {
				#print "***$raidlog[$j]";
				if($raidlog[$j] =~ /^seqNum:/) {
					last;
				}
				if($raidlog[$j] =~ /^Time:\s+(\S+.*)/) {
					$datetime = $1;
				}

				if($raidlog[$j] =~ /^Event\s+Description:\s+(\S+.*)/) {
					$logdesc = $1;
				}
		
				if($raidlog[$j] =~ /^Event\s+Data:/) {
					$logdata = $raidlog[$j + 2];
				}
			}
		}
		if(!$datetime) { next; }
		if(!$logdesc) { next; }
		if($logdesc =~ /^Time\s+established/) { next; }
		if($logdesc =~ /^Foreign\s+Configuration\s+Detected/) { next; }
		if($logdata =~ /^None/) { next; }
			
		$loginfo = $datetime."|| ".$logdesc."\n";
		push(@raidlogarr, $loginfo);
	}
	my @newlogarr = reverse (@raidlogarr);
	open(FL, ">/var/log/nas/RaidLog");
	foreach(@newlogarr) {
		print FL $_."\n";
	}
	close(FL);
	return @newlogarr;
}

sub list_raid_log
{
@rdlog=&get_raid_log();
$aa=@rdlog;
print "<table width=100%>";

print "<tr>";
print "<td>";

print "<div class=main>";
print "<table class=mode1 border=0 cellspacing=0 cellpadding=0 width='100%'>\n";
print "<tr class=title1>";
#print "<td><b>&nbsp;&nbsp;&nbsp;&nbsp;$text{'table_type'}</b></td>";
#print "<td><b>$text{'table_date'}</b></td>";
print "<td><b>$text{'table_time'}</b></td>";
#print "<td><b>$text{'table_source'}</b></td>";
print "<td><b>$text{'table_event'}</b></td>";
print "</tr>\n";
print "<form method=post onsubmit='return onsubmit_form();'>";

foreach (@rdlog){
        my ($datetime,$event)=split(/\|\|/, $_);


        $mode = ( $mode==2 ? 1:2 );
        print "<tr class=mode$mode>";
        #print "<td><input type=radio $tmp ";
        #print " value=$i name=line_no>";
        #print "&nbsp;$type</td>";
        print "<td>";
	print $datetime;
        #printf "%d-%02d-%02d",$yy,$mm,$date;
        print "</td>";
        #print "<td>",$time,"</td>";
        #print "<td>",$source,"</td>";
        print "<td>",$event,"</td>";
        print "</tr>\n";
}


print "</TABLE>";
print "</div>";
print "</td>";

print "<td valign=top width=120>";
print "<table width=100% cellpadding=4>";

#print "<tr><td>&nbsp;</td></tr>";
print "<input type=hidden name=log value='raidlog'>";
#print "<tr><td>";
#print "<input type=submit name=event class=button ";
#print " value='$text{'event_details'}' onclick='event_details();' $disable >";
#print "</td></tr>";

print "<tr><td>";
print "<input type=submit name=download class=button ";
print " value='$text{'download_log'}' onclick='download_log();'>";
print "</td></tr>";

#print "<tr><td>";
#print "<input type=submit name=setup class=button ";
#print " value='$text{'log_properties'}' onclick='log_properties();'>";
#print "</td></tr>";

#print "<tr><td>";
#print "<input type=submit name=clear class=button ";
#print " value='$text{'clear_log'}' onclick='clear_log();' $disable >";
#print "</td></tr>";
print "</form>";

print "<tr height=100%><td>&nbsp;</td></tr>";

print "</table>";
print "</td>";
print "</tr>";
print "</table>";

print <<EOF;
<script>
function log_properties()
{
        document.forms[0].action="log_properties.cgi";
}

function download_log()
{
        document.forms[0].action="download_log.cgi";
}

function event_details()
{
        document.forms[0].action="event_details.cgi";
}
function setv(lv)
{
        document.forms[0].log_no.value=lv;
}
function clear_log()
{
        if (!confirm("$text{'clear_log_confirm'}")) return false;
        document.forms[0].action="clear_log.cgi";
}
function onsubmit_form()
{
        document.forms[0].event.disabled = true;
        document.forms[0].download.disabled = true;
        document.forms[0].setup.disabled = true;
        document.forms[0].clear.disabled = true;
}

</script>
EOF

}

sub list_log
{
my $logname=shift;
my $mode=1;
my %smonth= ('Jan',1, 'Feb',2, 'Mar',3, 'Apr',4,  'May',5,  'Jun',6,
             'Jul',7, 'Aug',8, 'Sep',9, 'Oct',10, 'Nov',11, 'Dec',12 );
my $filename=$config{$logname};

open(FH,"< $filename");
@tmpniu = <FH>;
@tmpniu2 = reverse (@tmpniu);
close(FH);
print "<table width=100%>";

print "<tr>";
print "<td>";

print "<div class=main>"; 
print "<table class=mode1 border=0 cellspacing=0 cellpadding=0 width='100%'>\n";
print "<tr class=title1>";
print "<td><b>&nbsp;&nbsp;&nbsp;&nbsp;$text{'table_type'}</b></td>";
print "<td><b>$text{'table_date'}</b></td>";
print "<td><b>$text{'table_time'}</b></td>";
print "<td><b>$text{'table_source'}</b></td>";
print "<td><b>$text{'table_event'}</b></td>";
print "</tr>\n";
print "<form method=post onsubmit='return onsubmit_form();'>";
my @tm=localtime();
my $i=0;
my $disable='disabled=true';
foreach (@tmpniu2){
	my ($month,$date,$time,$host,$source,$type,$event)=split(' ');
	if(! defined $smonth{$month} ) { next; } 
	if ( $type !~ /crit|info|warning|debug/ ) { next; }
	if ( $event !~ /\d+/ ) { next; }
	my $tmp=' '; 

	$yy=$tm[5]+1900;
	$mm=$smonth{$month};
	if($mm>($tm[4]+1)){ $yy--; }
	
	$i++;

	if ($i==1) { 
		$tmp="checked"; 
	}
	$source =~ s/\[\d+\]://;
	if(($event == 12020) || ($event == 12021) || ($event == 12051) || ($event == 12053) || ($event == 12080) || ($event == 12081))
        {
           
           $source = "net";
        }
	$disable=' ';

	$mode = ( $mode==2 ? 1:2 );
	print "<tr class=mode$mode>";
	print "<td><input type=radio $tmp ";
	print " value=$i name=line_no>";
	print "&nbsp;$type</td>";
	print "<td>";
	
	printf "%d-%02d-%02d",$yy,$mm,$date;
	print "</td>";
	print "<td>",$time,"</td>";
	print "<td>",$source,"</td>";
	print "<td>",$event,"</td>";
	print "</tr>\n";
}
#close(FH);

print "</TABLE>";
print "</div>";
print "</td>";

print "<td valign=top width=120>";
print "<table width=100% cellpadding=4>";

#print "<tr><td>&nbsp;</td></tr>";
print "<input type=hidden name=log value='$logname'>";
print "<tr><td>";
print "<input type=submit name=event class=button ";
print " value='$text{'event_details'}' onclick='event_details();' $disable >";
print "</td></tr>";

print "<tr><td>";
print "<input type=submit name=download class=button ";
print " value='$text{'download_log'}' onclick='download_log();'>";
print "</td></tr>";

print "<tr><td>";
print "<input type=submit name=setup class=button ";
print " value='$text{'log_properties'}' onclick='log_properties();'>";
print "</td></tr>";

print "<tr><td>";
print "<input type=submit name=clear class=button ";
print " value='$text{'clear_log'}' onclick='clear_log();' $disable >";
print "</td></tr>";
print "</form>";

print "<tr height=100%><td>&nbsp;</td></tr>";

print "</table>";
print "</td>";
print "</tr>";
print "</table>";

print <<EOF;
<script>
function log_properties()
{
	document.forms[0].action="log_properties.cgi";
}

function download_log()
{
	document.forms[0].action="download_log.cgi";
}

function event_details()
{
	document.forms[0].action="event_details.cgi";
}
function setv(lv)
{
	document.forms[0].log_no.value=lv;
}
function clear_log()
{
	if (!confirm("$text{'clear_log_confirm'}")) return false;
	document.forms[0].action="clear_log.cgi";
}
function onsubmit_form()
{
        document.forms[0].event.disabled = true;
        document.forms[0].download.disabled = true;
        document.forms[0].setup.disabled = true;
        document.forms[0].clear.disabled = true;
}

</script>
EOF
}

1;

